import glob
import os
import time
import shutil
import pandas as pd
from 筛选excel import excel
from DrissionPage.common import Keys


def get_data_only(page, down_path, date):
    shop_name = '抖音小店-' + page(".index_userName__16Isl").text
    if os.path.exists(f"C:\\抖音客服数据\\{date}\\{shop_name}.xlsx"):
        return
    page('接待').click()
    time.sleep(8)
    # 切换到最新打开的标签页
    tab = page.get_tab(page.latest_tab)
    if tab.s_eles("退出引导"):
        tab("退出引导").click()
        time.sleep(1)
    if tab.s_eles('.qg8dJkHI2UbLch1KK8J6'):
        tab('.qg8dJkHI2UbLch1KK8J6').ele("tag:img").click()
        time.sleep(1)
    tab("数据").click()
    tab("客服数据").click.multiple(2)
    tab("历史数据").click()
    time.sleep(3)
    tab('@placeholder=开始时间').input(date)
    tab('@placeholder=结束时间').input(date)
    time.sleep(1)
    tab.actions.key_down(Keys.ENTER)
    # tab("近1天",timeout=3).click()
    tab("查询").click()
    time.sleep(3)
    if not tab('@class:auxo-btn auxo-btn-dashed').states.is_enabled:
        print('没有导出权限')
        df = pd.DataFrame()
        df.to_excel(f"C:\\抖音客服数据\\{date}\\{shop_name}.xlsx")
        return
    tab("导出报表").click(by_js=True)
    time.sleep(3)
    # 获取文件夹中所有的.xlsx文件
    xlsx_files = glob.glob(os.path.join(down_path, "*.xlsx"))
    os.makedirs(f'C:\\抖音客服数据\\{date}', exist_ok=True)
    shutil.move(xlsx_files[0], f"C:\\抖音客服数据\\{date}\\{shop_name}.xlsx")
    excel(shop_name, date, f"C:\\抖音客服数据\\{date}\\{shop_name}.xlsx")
